package com.wenboit.initproject.config.shiro;

import com.wenboit.initproject.common.shiro.CustomerRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * (ShiroConfig). 
 *
 * @author 汪文波(Wang Wenbo) wenboit@163.com
 * @notes Create on 2021-12-13 16:39
 */
@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean factory = new ShiroFilterFactoryBean();

        factory.setSecurityManager(manager);

        Map<String, String> filter = new HashMap<>();
        filter.put("/user/login", "anon");
        filter.put("/**", "authc");

        factory.setLoginUrl("/login.jsp");
        factory.setFilterChainDefinitionMap(filter);

        return factory;
    }

    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("getRealm") Realm realm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(realm);

        return manager;
    }


    @Bean
    public Realm getRealm(){
        CustomerRealm realm = new CustomerRealm();
        return realm;
    }

}
